package hr.fer.zpr.nmbp.project3.dao.util;


import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.*;


import javax.xml.parsers.*;

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;


import hr.fer.zpr.nmbp.project3.model.Bolest;
import hr.fer.zpr.nmbp.project3.model.Bolnica;
import hr.fer.zpr.nmbp.project3.model.Dijagnoza;
import hr.fer.zpr.nmbp.project3.model.Karton;
import hr.fer.zpr.nmbp.project3.model.Lijecnik;
import hr.fer.zpr.nmbp.project3.model.Pacijent;

public class XMLGen {

	public static String getXML(Bolnica bolnica) {
		StringBuilder sb = new StringBuilder();

		// XML
		sb.append("<bolnica sifra=\"").append(bolnica.getSifra()).append("\">");
		// adresa
		sb.append("<adresa>");
		sb.append(bolnica.getAdresa() == null ? "" : bolnica.getAdresa());
		sb.append("</adresa>");
		// naziv
		sb.append("<naziv>");
		sb.append(bolnica.getNaziv() == null ? "" : bolnica.getNaziv());
		sb.append("</naziv>");
		sb.append("</bolnica>");

		return sb.toString();
	}

	public static String getXML(Lijecnik lijecnik) {
		Document doc = XMLGen.createDocument();

		Element root = doc.createElement("lijecnik");
		root.setAttribute("bolnica", Integer.toString(lijecnik.getBolnica().getSifra()));
		root.setAttribute("sifra", Integer.toString(lijecnik.getSifra()));		
		doc.appendChild(root);

		Element ime = doc.createElement("ime");
		ime.setTextContent(lijecnik.getIme());
		root.appendChild(ime);

		Element prezime = doc.createElement("prezime");
		prezime.setTextContent(lijecnik.getPrezime());
		root.appendChild(prezime);

		System.out.println(XMLGen.createXML(doc));
		return XMLGen.createXML(doc);
	}

	public static String getXML(Pacijent pacijent) {
		Document doc = XMLGen.createDocument();

		Element root = doc.createElement("pacijent");
		root.setAttribute("lijecnik",
				Integer.toString(pacijent.getLijecnik().getSifra()));
		root.setAttribute("mbo", Integer.toString(pacijent.getMbo()));
		doc.appendChild(root);

		Element ime = doc.createElement("ime");
		ime.setTextContent(pacijent.getIme());
		root.appendChild(ime);

		Element prezime = doc.createElement("prezime");
		prezime.setTextContent(pacijent.getPrezime());
		root.appendChild(prezime);

		
		Element karton = doc.createElement("karton");
		karton = XMLGen.createKarton(root.getOwnerDocument(), karton, pacijent.getKarton());

		root.appendChild(karton);
		
		System.out.println(XMLGen.createXML(doc));
		return XMLGen.createXML(doc);
	}

	public static String getXML(Karton karton) {
		Document doc = XMLGen.createDocument();
		
		Element root = doc.createElement("karton");		
		doc.appendChild(root);
		
		XMLGen.createKarton(doc, root, karton);
				
		return XMLGen.createXML(doc);
	}
	
	private static Element createKarton(Document doc, Element root, Karton karton) {
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");

		Element datumRodenja = doc.createElement("datumRodenja");
		datumRodenja.setTextContent(dateFormater.format(karton.getDatumRodenja()));
		root.appendChild(datumRodenja);
		
		for (Dijagnoza d : karton.getDijagnoze()) {
			Element dijagnoza = doc.createElement("dijagnoza");
			dijagnoza.setTextContent(XMLGen.getXML(d));
			root.appendChild(dijagnoza);
		}
		
		Element kilaza = doc.createElement("kilaza");
		kilaza.setTextContent(karton.getKilaza().toString());
		root.appendChild(kilaza);
		
		Element krvnaGrupa = doc.createElement("krvnaGrupa");
		krvnaGrupa.setTextContent(karton.getKrvnaGrupa().toString());
		root.appendChild(krvnaGrupa);
		
		Element napomena = doc.createElement("napomena");
		napomena.setTextContent(karton.getNapomena());
		root.appendChild(napomena);
		
		Element spol = doc.createElement("spol");
		spol.setTextContent(karton.getSpol().toString());
		root.appendChild(spol);
		
		return root;
	}

	public static String getXML(Bolest bolest) {
		Document doc = XMLGen.createDocument();
		
		Element root = doc.createElement("bolest");
		root.setAttribute("sifra", Integer.toString(bolest.getSifra()));		
		doc.appendChild(root);

		Element naziv = doc.createElement("naziv");
		naziv.setTextContent(bolest.getNaziv());
		root.appendChild(naziv);

		Element opis = doc.createElement("opis");
		opis.setTextContent(bolest.getOpis());
		root.appendChild(opis);
		
		return XMLGen.createXML(doc);
	}

	public static String getXML(Dijagnoza dijagnoza) {
		Document doc = XMLGen.createDocument();
		
		Element root = doc.createElement("dijagnoza");
		root.setAttribute("sifra", Integer.toString(dijagnoza.getSifra()));		
		doc.appendChild(root);

		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
		
		Element datum = doc.createElement("naziv");
		datum.setTextContent(dateFormater.format(dijagnoza.getDatum()));
		root.appendChild(datum);

		Element opis = doc.createElement("opis");
		opis.setTextContent(dijagnoza.getOpis());
		root.appendChild(opis);
		
		return XMLGen.createXML(doc);
	}
	
	public static List<String> getXMLBolestDijagnoza(Dijagnoza dijagnoza) {
		List<String> elements = new ArrayList<String>();
		
		for (Bolest bolest : dijagnoza.getBolesti()) {
			Document doc = XMLGen.createDocument();
			
			Element root = doc.createElement("dijagnozaBolest");			
			root.setAttribute("dijagnoza", Integer.toString(dijagnoza.getSifra()));		
			root.setAttribute("bolest", Integer.toString(bolest.getSifra()));
			
			doc.appendChild(root);
			
			elements.add(XMLGen.createXML(doc));
		}

		return 	elements;	
	}

	private static Document createDocument() {
		DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder;
		try {
			docBuilder = dbfac.newDocumentBuilder();
			Document doc = docBuilder.newDocument();
			return doc;
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private static String createXML(Document doc) {
		try {
			TransformerFactory transfac = TransformerFactory.newInstance();
			Transformer trans = transfac.newTransformer();
			trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			trans.setOutputProperty(OutputKeys.INDENT, "yes");

			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			DOMSource source = new DOMSource(doc);

			trans.transform(source, result);
			String xmlString = sw.toString();
			return xmlString;
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}


}
